﻿#include <SFML/Graphics.hpp>
#include <iostream>

using namespace sf;
using namespace std;

// -------------------------------
// 🎮 游戏配置
// -------------------------------
const float TILE_SIZE = 100;
const int R = 10;
const int GRID_WIDTH = 5;
const int GRID_HEIGHT = 5;
const int WINDOW_WIDTH = GRID_WIDTH * TILE_SIZE;
const int WINDOW_HEIGHT = GRID_HEIGHT * TILE_SIZE;

// -------------------------------
// 🧩 地图格子类型
// -------------------------------
enum CellType {
    EMPTY,     // 可行走的空地
    WALL,      // 墙壁，不可通过
    PLAYER,    // 玩家位置
    EXIT       // 出口，到达即胜利
};

// -------------------------------
// 🗺️ 游戏地图（5x5 网格）
// -------------------------------
// 玩家起始位置：(1,1)
// 出口位置：(3,4) —— 第4行第5列
CellType gameMap[GRID_HEIGHT][GRID_WIDTH] = {
    // y=0（上边界，墙）
    {WALL,  WALL,  WALL,  WALL,  WALL},
    // y=1
    {WALL,  EMPTY, EMPTY,  EMPTY,  WALL},
    // y=2
    {WALL,  EMPTY,  EMPTY,  EMPTY,  WALL},
    // y=3
    {WALL,  EMPTY,  EMPTY,  EMPTY,  EXIT},
    // y=4（下边界，墙）
    {WALL,  WALL,  WALL,  WALL,  WALL}
};

// 玩家坐标
int playerX = 1;
int playerY = 1;

// -------------------------------
// 🖼️ SFML 窗口
// -------------------------------
RenderWindow window(VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "Arrows Puzzle Escape");

// 每个格子的绘制背景
RectangleShape tileBg(Vector2f(TILE_SIZE-2, TILE_SIZE-2));

// 颜色定义
const Color COLOR_EMPTY(200, 200, 200);    // 空地：浅灰
const Color COLOR_WALL(100, 100, 100);     // 墙：深灰
const Color COLOR_PLAYER(0, 0, 255);       // 玩家：蓝色圆形
const Color COLOR_EXIT(0, 255, 0);         // 出口：绿色

// -------------------------------
// 🧩 绘制单个格子
// -------------------------------
void drawCell(int x, int y, CellType type) {
    tileBg.setPosition(x * TILE_SIZE, y * TILE_SIZE);
    tileBg.setFillColor(COLOR_EMPTY); // 默认

    switch (type) {
    case WALL:    tileBg.setFillColor(COLOR_WALL); break;
    case PLAYER:  tileBg.setFillColor(COLOR_PLAYER); break;
    case EXIT:    tileBg.setFillColor(COLOR_EXIT); break;
    }

    window.draw(tileBg);
}

// -------------------------------
// 🎮 主游戏循环
// -------------------------------
int main() {
    while (window.isOpen()) {
        Event event;
        while (window.pollEvent(event)) {
            if (event.type == Event::Closed)
                window.close();

            // ✅ 只监听按键按下事件（不是按住，不是释放！）
            if (event.type == Event::KeyPressed) {
                bool moved = false;

                if (event.key.code == Keyboard::Up) {
                    int newY = playerY - 1;
                    if (newY >= 0 && gameMap[newY][playerX] != WALL) {
                        gameMap[playerY][playerX] = EMPTY;
                        playerY = newY;
                        moved = true;
                    }
                }
                else if (event.key.code == Keyboard::Down) {
                    int newY = playerY + 1;
                    if (newY < GRID_HEIGHT && gameMap[newY][playerX] != WALL) {
                        gameMap[playerY][playerX] = EMPTY;
                        playerY = newY;
                        moved = true;
                    }
                }
                else if (event.key.code == Keyboard::Left) {
                    int newX = playerX - 1;
                    if (newX >= 0 && gameMap[playerY][newX] != WALL) {
                        gameMap[playerY][playerX] = EMPTY;
                        playerX = newX;
                        moved = true;
                    }
                }
                else if (event.key.code == Keyboard::Right) {
                    int newX = playerX + 1;
                    if (newX < GRID_WIDTH && gameMap[playerY][newX] != WALL) {
                        gameMap[playerY][playerX] = EMPTY;
                        playerX = newX;
                        moved = true;
                    }
                }

                // ✅ 可选：打印调试信息
                if (moved) {
                    std::cout << "移动成功！当前位置: (" << playerX << ", " << playerY << ")" << std::endl;
                }
            }
        }

        if (gameMap[playerY][playerX] == EXIT) {
            window.close();
            cout << "🎉 恭喜你！成功手动移动到出口，逃脱成功！" << endl;
            return 0;
        }

        // -------------------- 🖼️ 绘制画面 --------------------
        window.clear(Color::White);

        // 绘制所有格子
        for (int y = 0; y < GRID_HEIGHT; y++) {
            for (int x = 0; x < GRID_WIDTH; x++) {
                drawCell(x, y, gameMap[y][x]);
            }
        }

        // 玩家是一个半径为 30 的圆
        CircleShape player(R); // 半径是 30，直径 60，看起来适中
        //player.setRadius(20.f);

        // 设置填充颜色
        player.setFillColor(COLOR_PLAYER);

        // 计算当前格子的中心位置
        float centerX = playerX * TILE_SIZE + (TILE_SIZE - 2) / 2-R;
        float centerY = playerY * TILE_SIZE + (TILE_SIZE - 2) / 2-R;
        cout << centerX << centerY<<endl;
        // CircleShape 的 setPosition 是设置左上角
        player.setPosition(centerX, centerY);

        
        window.draw(player);
        
        window.display();
    }

    return 0;
}